package pub.guoxin.oops.manager.controller;

import com.google.common.base.Strings;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pub.guoxin.oops.annotation.InjectUser;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.enums.Authority;
import pub.guoxin.oops.manager.service.DeviceDataInfoService;
import pub.guoxin.oops.manager.service.RealTimeDeviceDataInfoService;
import pub.guoxin.oops.model.DeviceDataInfo;
import pub.guoxin.oops.model.RealTimeDeviceDataInfo;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * Created by guoxin on 17-11-5.
 */
@RestController
@RequestMapping(OopsPathConstants.DEVICE_PATH)
@PreAuthorize(value = "hasAnyRole('ADMIN,USER')")
public class DeviceDataInfoController {

    @Autowired
    private DeviceDataInfoService deviceDataInfoService;
    @Autowired
    private RealTimeDeviceDataInfoService realTimeDeviceDataInfoService;
    @Autowired
    private ResultVoBuilder builder;


    @ApiOperation(value = "历史数据", notes = "历史数据（用于历史数据的查询分页显示，权限：公司管理员，公司子帐户）")
    @GetMapping(value = "/{sn}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<Page<DeviceDataInfo>> page(@InjectUser User user,
                                               @PathVariable String sn,
                                               @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                               @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                               @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                                               @RequestParam(required = false, defaultValue = "15") Integer pageSize
//                                                ,@RequestParam(required = false) String sort
    ) {
        Page<DeviceDataInfo> page = deviceDataInfoService.page(sn, user, startTime, endTime, pageNumber, pageSize);
        return builder.buildSuccessResultInfo(page);
    }

    @ApiOperation(value = "一览表", notes = "一览表（权限：公司管理员，公司子帐户）")
    @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<Page<RealTimeDeviceDataInfo>> schedule(@InjectUser User user,
                                                           @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                                                           @RequestParam(required = false, defaultValue = "15") Integer pageSize,
                                                           @RequestParam(required = false) String groupId) {
        if (Authority.ROLE_USER.equals(user.findFirstAuthorityEnum()) && Strings.isNullOrEmpty(groupId)) {
            return  builder.buildBadRequestResultInfo("权限不足");
        }
        Page<RealTimeDeviceDataInfo> adminList = realTimeDeviceDataInfoService.schedule(user.getCompanyId(), pageNumber, pageSize, groupId);
        return builder.buildSuccessResultInfo(adminList);
    }

    @ApiOperation(value = "通过门站Id获得设备SN", notes = "通过门站Id获得设备SN（权限：公司管理员，公司子帐户）")
    @GetMapping(value = "/sns/{groupId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo getDeviceSns(@PathVariable String groupId) {
        Set<String> result = realTimeDeviceDataInfoService.getDeviceSns(groupId);
        return builder.buildSuccessResultInfo(result);
    }

    @ApiOperation(value = "修改备注", notes = "修改备注（权限：公司管理员，公司子帐户）")
    @PutMapping(value = "/{sn}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo modifyRemark(String sn, String remark) {
        if (realTimeDeviceDataInfoService.modifyRemark(sn, remark)) {
            return builder.buildSuccessResultInfo("修改成功");
        }
        return builder.buildBadRequestResultInfo("未找到该模块");
    }
}
